import pandas as pd
import time
import pymongo
import random


def fun(movie_country, movie_date, movie_length, movie_director, movie_star_1, movie_star_2, movie_class):
    myclient = pymongo.MongoClient('mongodb://47.93.220.108:27017/')
    mydb = myclient['movie']
    classes = {
        '恐怖': 1,
        '传记': 2,
        '歌舞': 3,
        '武侠': 4,
        '戏曲': 5,
        '灾难': 6,
        '剧情': 7,
        '动画': 8,
        '喜剧': 9,
        '纪录片': 10,
        '音乐': 11,
        '同性': 12,
        '短片': 13,
        '爱情': 14,
        '科幻': 15,
        '未知': 16,
        '家庭': 17,
        '历史': 18,
        '战争': 19,
        '悬疑': 20,
        '古装': 21,
        '奇幻': 22,
        '儿童': 23,
        '犯罪': 24,
        '惊悚': 25,
        '冒险': 26,
        '运动': 27,
        '动作': 28
    }
    data1 = pd.read_csv('model/data1.csv')
    data2 = pd.read_csv('model/data2.csv')
    data_country_quantification = pd.read_csv("model/data_country_quantification.csv")
    # 1,movie_country
    try:
        movie_country = float(
            data_country_quantification[data_country_quantification['production_area'] == movie_country][
                'movie_officeBox'])
    except:
        movie_country = 0

    # 2,datetime,格林尼治时间
    try:
        movie_date = int(time.mktime(time.strptime(movie_date, "%Y-%m-%d")))
    except:
        try:
            movie_date = int(time.mktime(time.strptime(movie_date, "%Y-%m")))
        except:
            movie_date = int(time.mktime(time.strptime(movie_date, "%Y")))
    # print(movie_date)

    # 3,时长
    # print(movie_length)

    # 4,导演
    movie_director = mydb.director.find({'name': movie_director})[0]['rank']
    # print(movie_director)

    # 5,演员
    try:
        movie_star_1 = mydb.actor.find({'actor': movie_star_1})[0]['rank']
    except:
        movie_star_1 = random.randint(10, 18)
    try:
        movie_star_2 = mydb.actor.find({'actor': movie_star_2})[0]['rank']
    except:
        movie_star_2 = random.randint(10, 18)
    # print(str(movie_star_1)+' '+str(movie_star_2))

    # 6,类型
    movie_class = classes[movie_class]
    # print(movie_class)

    res = {
        'movie_country': movie_country,
        'movie_date': movie_date,
        'movie_length': movie_length,
        'movie_director': movie_director,
        'movie_star_1': movie_star_1,
        'movie_star_2': movie_star_2,
        'movie_class': movie_class
    }
    print(res)
    return res
